package org.xbill.DNS.utils;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class base32
{
  private String alphabet;
  private boolean lowercase;
  private boolean padding;

  public base32(String paramString, boolean paramBoolean1, boolean paramBoolean2)
  {
    this.alphabet = paramString;
    this.padding = paramBoolean1;
    this.lowercase = paramBoolean2;
  }

  private static int blockLenToPadding(int paramInt)
  {
    int i;
    switch (paramInt)
    {
    default:
      i = -1;
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              return i;
              i = 6;
            }
            i = 4;
          }
          i = 3;
        }
        i = 1;
      }
      i = 0;
    }
  }

  private static int paddingToBlockLen(int paramInt)
  {
    int i;
    switch (paramInt)
    {
    case 2:
    case 5:
    default:
      i = -1;
    case 6:
    case 4:
    case 3:
    case 1:
    case 0:
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          while (true)
          {
            while (true)
            {
              return i;
              i = 1;
            }
            i = 2;
          }
          i = 3;
        }
        i = 4;
      }
      i = 5;
    }
  }

  public byte[] fromString(String paramString)
  {
    label93: int[] arrayOfInt;
    byte[] arrayOfByte3;
    int i2;
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    byte[] arrayOfByte1 = paramString.getBytes();
    int i = 0;
    while (true)
    {
      if (i >= arrayOfByte1.length)
      {
        if (!(this.padding))
          break;
        if (localByteArrayOutputStream.size() % 8 == 0)
          break label93;
        arrayOfByte3 = null;
        return arrayOfByte3;
      }
      char c = (char)arrayOfByte1[i];
      if (!(Character.isWhitespace(c)))
        localByteArrayOutputStream.write((byte)Character.toUpperCase(c));
      ++i;
    }
    do
      localByteArrayOutputStream.write(61);
    while (localByteArrayOutputStream.size() % 8 != 0);
    byte[] arrayOfByte2 = localByteArrayOutputStream.toByteArray();
    localByteArrayOutputStream.reset();
    DataOutputStream localDataOutputStream = new DataOutputStream(localByteArrayOutputStream);
    int j = 0;
    while (true)
    {
      int i1;
      while (j >= arrayOfByte2.length / 8)
        arrayOfByte3 = localByteArrayOutputStream.toByteArray();
      short[] arrayOfShort = new short[8];
      arrayOfInt = new int[5];
      int k = 8;
      int l = 0;
      while (true)
      {
        while (true)
        {
          if (l >= 8);
          do
            while (true)
            {
              i1 = paddingToBlockLen(k);
              if (i1 >= 0)
                break label243;
              arrayOfByte3 = null;
            }
          while ((char)arrayOfByte2[(l + j * 8)] == '=');
          arrayOfShort[l] = (short)this.alphabet.indexOf(arrayOfByte2[(l + j * 8)]);
          if (arrayOfShort[l] >= 0)
            break;
          arrayOfByte3 = null;
        }
        --k;
        ++l;
      }
      label243: arrayOfInt[0] = (arrayOfShort[0] << 3 | arrayOfShort[1] >> 2);
      arrayOfInt[1] = ((0x3 & arrayOfShort[1]) << 6 | arrayOfShort[2] << 1 | arrayOfShort[3] >> 4);
      arrayOfInt[2] = ((0xF & arrayOfShort[3]) << 4 | 0xF & arrayOfShort[4] >> 1);
      arrayOfInt[3] = (arrayOfShort[4] << 7 | arrayOfShort[5] << 2 | arrayOfShort[6] >> 3);
      arrayOfInt[4] = ((0x7 & arrayOfShort[6]) << 5 | arrayOfShort[7]);
      i2 = 0;
      if (i2 < i1)
        break;
      label366: ++j;
    }
    try
    {
      localDataOutputStream.writeByte((byte)(0xFF & arrayOfInt[i2]));
      ++i2;
    }
    catch (IOException localIOException)
    {
      break label366:
    }
  }

  public String toString(byte[] paramArrayOfByte)
  {
    int i1;
    int i2;
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    int i = 0;
    if (i >= (4 + paramArrayOfByte.length) / 5)
      return new String(localByteArrayOutputStream.toByteArray());
    short[] arrayOfShort = new short[5];
    int[] arrayOfInt = new int[8];
    int j = 5;
    int k = 0;
    if (k >= 5)
    {
      int l = blockLenToPadding(j);
      arrayOfInt[0] = (byte)(0x1F & arrayOfShort[0] >> 3);
      arrayOfInt[1] = (byte)((0x7 & arrayOfShort[0]) << 2 | 0x3 & arrayOfShort[1] >> 6);
      arrayOfInt[2] = (byte)(0x1F & arrayOfShort[1] >> 1);
      arrayOfInt[3] = (byte)((0x1 & arrayOfShort[1]) << 4 | 0xF & arrayOfShort[2] >> 4);
      arrayOfInt[4] = (byte)((0xF & arrayOfShort[2]) << 1 | 0x1 & arrayOfShort[3] >> 7);
      arrayOfInt[5] = (byte)(0x1F & arrayOfShort[3] >> 2);
      arrayOfInt[6] = (byte)((0x3 & arrayOfShort[3]) << 3 | 0x7 & arrayOfShort[4] >> 5);
      arrayOfInt[7] = (byte)(0x1F & arrayOfShort[4]);
      i1 = 0;
      if (i1 < arrayOfInt.length - l)
        break label302;
      if (this.padding)
        i2 = arrayOfInt.length - l;
    }
    while (true)
    {
      while (true)
      {
        while (true)
        {
          if (i2 < arrayOfInt.length)
            break label342;
          ++i;
        }
        if (k + i * 5 < paramArrayOfByte.length)
          arrayOfShort[k] = (short)(0xFF & paramArrayOfByte[(k + i * 5)]);
        while (true)
        {
          while (true)
            ++k;
          arrayOfShort[k] = 0;
          --j;
        }
        label302: char c = this.alphabet.charAt(arrayOfInt[i1]);
        if (this.lowercase)
          c = Character.toLowerCase(c);
        localByteArrayOutputStream.write(c);
        ++i1;
      }
      label342: localByteArrayOutputStream.write(61);
      ++i2;
    }
  }

  public static class Alphabet
  {
    public static final String BASE32 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=";
    public static final String BASE32HEX = "0123456789ABCDEFGHIJKLMNOPQRSTUV=";
  }
}